package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.impl;

import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.entity.Lifeline;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ReportPublicField;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/05/23
 * @Description:
 */
@Repository
public class ReportPublicFieldRepositoryImpl implements ReportPublicFieldRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_report_public_field"; // 表名

    //根据报表id删除
    @Override
    public void deleteByXlrmId(String previousXlrmId) {
        String sql = "delete from  "+TABLE_NAME+"  where xlrm_id= '"+previousXlrmId+"'" ;
        jdbcTemplate.update(sql);
    }

    @Override
    public void save(ReportPublicField reportPublicField) {
        String sql = "insert into "+TABLE_NAME+" (xlrm_id,xsem_id,report_type,province,city,county,township,enterprise_name,create_user,del_flag) " +
                "values(:xlrmId,:xsemId,:reportType,:province,:city,:county,:township,:enterpriseName,:createUser,:delFlag)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(reportPublicField);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<ReportPublicField> getReportPublicFieldGroupByCounty(String xsemId) {
        String sql = "select province,city,county from xczh_report_public_field where xsem_id='"+xsemId+"' group by province,city,county";
        List<ReportPublicField> reportPublicFields = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ReportPublicField.class));
        return reportPublicFields;
    }

    @Override
    public List<ReportPublicField> getReportPublicFieldGroupByEnterpriseName(String xsemId) {
        String sql = "select enterprise_name from xczh_report_public_field group by enterprise_name";
        List<ReportPublicField> reportPublicFields = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ReportPublicField.class));
        return reportPublicFields;
    }
    //根据传入id修改删除标记为可见
    @Override
    public void update(List<String> idList) {
        String sql="update "+TABLE_NAME+" set del_flag = '0 ' " +
                " where xlrm_id in ('"+String.join("','", idList)+"')";
        jdbcTemplate.update(sql);
    }
}
